package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增加指定门客门客实力
 */
public class UseAddTargetPatronsAbility extends BaseUseGoods {

	public UseAddTargetPatronsAbility(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		int patronsId = goodsInfo.getParamList().get(0).intValue();
		UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
		if(null == userPatrons){
			return GameErrorCode.E_PATRONS_NO_FOUND;
		}
		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		int patronsId = goodsInfo.getParamList().get(0).intValue();
		UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
		if(null == userPatrons){
			result.setRet(GameErrorCode.E_PATRONS_NO_FOUND);
			return result;
		}
		Property property = new Property();
		BigInteger oldValue = userPatrons.getAddAbility();
		BigInteger addAbility = BigInteger.ZERO;
		for (long i = 0; i < count; i++) {
			BigInteger addRandomValue = GoodsMgr.getAddRandomValue(goodsInfo);
			addAbility = addAbility.add(addRandomValue);
		}
		property.addProperty(patronsId, addAbility);
		BigInteger newAbility = userPatrons.getAddAbility().add(addAbility);
		userPatrons.setAddAbility(newAbility);

		player.getModule(PatronsModule.class).onPatronsChange(userPatrons);
		player.getModule(PatronsModule.class).syncPatronsData(userPatrons);

		LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), patronsId, addAbility.longValue(),
				oldValue, userPatrons.getAddAbility(), (int)count);

		result.setReward(PropertyHelper.parsePropertyToString(property));
		return result;
	}
}
